<?php
/**
 * jishupu-cms
 * @Author:技术铺<jishupu@qq.com>
 * @Date: 2022/3/26
 */

namespace App\Http\Requests;

use App\Services\CacheService;

/**
 * 留言咨询表单验证
 * Class GuestbookRequest
 * @package App\Http\Requests\Web
 */
class GuestbookRequest extends BaseRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'vcode' => ['required', function ($attribute, $value, $fail) {
                $code = CacheService::pull(request('vcode_id') ?? 0);
                if (!hash_equals(strtolower($code), strtolower($value))) {
                    $fail(__('guestbook.error_vcode'));
                }
            }],
            'customer_name' => 'required|min:2|max:20',
            'customer_phone_number' => 'required|regex:/^1[345789][0-9]{9}$/',
            'customer_email' => 'required|email',
            'message' => 'required|max:300'
        ];
    }

    /**
     * 定义场景
     *
     * @var \string[][]
     */
    public $scenes = [
        'store' => ['vcode', 'customer_name', 'customer_phone_number', 'customer_email', 'message', 'vcode'],
    ];

    /**
     * 验证错误的自定义属性
     *
     * @return array
     */
    public function attributes()
    {
        return [
            'vcode' => __('guestbook.vcode'),
            'customer_name' => __('guestbook.customer_name'),
            'customer_phone_number' => __('guestbook.customer_phone_number'),
            'customer_email' => __('guestbook.customer_email'),
            'message' => __('guestbook.message')
        ];
    }

}
